/*
 * Copyright (c) 2020, HiHope Community.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "ui_hal.h"

#include <stdio.h>
#include <unistd.h>

#include "cmsis_os2.h"
#include "wifiiot_adc.h"

#define US_PER_S (1000*1000)

#define ADC_RESOLUTION (1<<12)

static uint16_t GetKeyAdcValue(void)
{
    uint16_t data = 0;
    AdcRead(WIFI_IOT_ADC_CHANNEL_2, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
    return data;
}

#define ABS(expr) ((expr) > 0 ? (expr) : -(expr))

// ADC = (V / (1.8*4)) * 4096
static KeyId AdcValue2KeyId(uint16_t adc)
{
    if (adc < 228) { // [0, 0.4) V
        return K_USER;
    } else if (adc < 455) { // [0.4, 0.8) V
        return K_LEFT;
    } else if (adc < 910) { // [0.8, 1.6) V
        return K_RIGHT;
    }
    return K_NONE;
}

KeyEvent HalGetKeyEvent(void)
{
    int adcValue = GetKeyAdcValue();
    static int lastAdcValue = 0;
    if (lastAdcValue == 0) {
        lastAdcValue = adcValue;
        adcValue = GetKeyAdcValue();
    }

    KeyEvent event = {K_NONE};
    if (ABS(adcValue - lastAdcValue) > 114) { // check both edge, delta voltage > 0.2 V
        if (adcValue < lastAdcValue) { // falling edge is caused key pressed event.
            event.type = KEY_PRESSED;
        } else {
            event.type = KEY_RELEASED;
        }

        if (event.type == KEY_PRESSED) {
            event.key = AdcValue2KeyId(adcValue);
        } else {
            event.key = AdcValue2KeyId(lastAdcValue);
        }
        LOGD("adc current: %d, last: %d", adcValue, lastAdcValue);
        LOGD("key: %d, type: %d", event.key, event.type);
    }

    lastAdcValue = adcValue;
    return event;
}


void HalDelayUs(unsigned us)
{
    usleep(us);
}

unsigned HalGetCurrentTimeUs(void)
{
    uint32_t stsPerUs = osKernelGetSysTimerFreq() / US_PER_S;
    return osKernelGetSysTimerCount() / stsPerUs;
}
